package com.ar4j.bench.ar4j.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ar4j.bench.ar4j.domain.Category;
import com.ar4j.bench.common.domain.CategoryWithProductsWithItems;
import com.ar4j.bench.common.domain.ICategory;
import com.ar4j.bench.common.domain.IProduct;
import com.ar4j.bench.common.domain.ProductWithItems;
import com.ar4j.bench.common.service.ICategoryService;
import com.ar4j.spring.AutowiredAdapter;
import com.ar4j.spring.SpringActiveRecordAdapter;

/**
 * A category service implemented using the AR4J library
 */
@Service
public class CategoryAr4jService implements ICategoryService {
  
  @AutowiredAdapter
  private SpringActiveRecordAdapter<Category> categoryAdapter;
  
  @Autowired
  private ProductAr4jService productService;
  
  @Override
  public Long addCategory(ICategory category) {
    Category cur = (Category) category;
    cur.save();
    return cur.getId();
  }
  
  @Override
  public ICategory findById(Long id) {
    return categoryAdapter.readOnly().findByIdentifier(id);
  }
  
  @Override
  public void removeById(Long id) {
    categoryAdapter.readOnly().deleteByIdentifier(id);
  }
  
  @Override
  public void updateCategory(ICategory category) {
    Category cur = (Category) category;
    cur.save();
  }
  
  public Long addCategoryWithProductsWithItems(CategoryWithProductsWithItems category) {
    Long categoryId = addCategory(category.getCategory());
    for(ProductWithItems product : category.getProducts()) {
      product.getProduct().setCategoryId(categoryId);
      productService.addProductWithItems(product);
    }
    return categoryId;
  }
  
  public CategoryWithProductsWithItems findCategoryWithProductsWithItemsById(Long id) {
    CategoryWithProductsWithItems out = new CategoryWithProductsWithItems();
    out.setCategory(findById(id));
    out.setProducts(productService.findProductWithItemsByCategoryId(id));
    return out;
  }
  
  public void updateCategoryWithProductsWithItems(CategoryWithProductsWithItems category) {
    updateCategory(category.getCategory());
    
    Map<Long, IProduct> fromDb = new HashMap<Long, IProduct>();
    for(IProduct product : productService.findByCategoryId(category.getCategory().getId())) {
      fromDb.put(product.getId(), product);
    }
    
    for(ProductWithItems product : category.getProducts()) {
      if(product.getProduct().getId() == null || !fromDb.containsKey(product.getProduct().getId())) {
        productService.addProductWithItems(product);
      } else {
        productService.updateProductWithItems(product);
        fromDb.remove(product.getProduct().getId());
      }
    }
    
    for(IProduct product : fromDb.values()) {
      productService.removeProductWithItemsById(product.getId());
    }
  }
  
  public void removeCategoryWithProductsWithItemsById(Long id) {
    for(IProduct product : productService.findByCategoryId(id)) {
      productService.removeProductWithItemsById(product.getId());
    }
    
    removeById(id);
  }

}
